package org.atguigu.gmall.realtime.app.dws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.AllWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.atguigu.gmall.realtime.app.BaseApp;
import org.atguigu.gmall.realtime.bean.UserRegisterBean;
import org.atguigu.gmall.realtime.common.Constant;
import org.atguigu.gmall.realtime.util.AtguiguUtil;
import org.atguigu.gmall.realtime.util.FlinkSinkUtil;

import java.time.Duration;

/**
 * Project:gmallRealTime
 * Package:org.atguigu.gmall.realtime.app.dws
 * Author: ZengHaiFeng
 * CreateTime:2023/5/4 19:03:30
 * Description: Todo
 * Version：1.0
 */
public class Dws_05_DwsUserUserRegisterWindow extends BaseApp {
    public static void main(String[] args) {
        new Dws_05_DwsUserUserRegisterWindow().init(40005,2,
                Constant.TOPIC_DWD_USER_REGISTER,"Dws_05_DwsUserUserRegisterWindow");
    }
    @Override
    public void handle(StreamExecutionEnvironment env, DataStreamSource<String> ds) {
       ds.map(JSON::parseObject)
               .assignTimestampsAndWatermarks(WatermarkStrategy
                       .<JSONObject>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                       .withTimestampAssigner((obj,ts) -> obj.getLong("create_time"))
                       .withIdleness(Duration.ofSeconds(60)))
               .windowAll(TumblingEventTimeWindows.of(Time.seconds(5)))
               .aggregate(new AggregateFunction<JSONObject, Long, Long>() {
                   @Override
                   public Long createAccumulator() {
                       return 0L;
                   }

                   @Override
                   public Long add(JSONObject value, Long accumulator) {
                       return accumulator+1;
                   }

                   @Override
                   public Long getResult(Long accumulator) {
                       return accumulator;
                   }

                   @Override
                   public Long merge(Long a, Long b) {
                       return a + b;
                   }
               }, new ProcessAllWindowFunction<Long, UserRegisterBean, TimeWindow>() {
                   @Override
                   public void process(
                           Context context,
                           Iterable<Long> values,
                           Collector<UserRegisterBean> out) throws Exception {
                       Long registerCt = values.iterator().next();
                       out.collect(new UserRegisterBean(
                               AtguiguUtil.tsToDateTime(context.window().getStart()),
                               AtguiguUtil.tsToDateTime(context.window().getEnd()),
                               registerCt,
                               System.currentTimeMillis()));
                   }
               })
               .addSink(FlinkSinkUtil.getClickHouseSink("dws_user_user_register_window",
                       UserRegisterBean.class));
    }
}
